package com.example.spring_project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spring_project.entity.LoginBody;
import com.example.spring_project.entity.User;
import com.example.spring_project.mapper.UserMapper;
import com.example.spring_project.service.CaptchaService;
import com.example.spring_project.service.UserService;
import com.example.spring_project.utils.Result;
import com.example.spring_project.utils.TokenUil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    private final UserMapper userMapper;
    @Autowired
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Autowired
    public CaptchaService captchaService;
    @Autowired
    public TokenUil tokenUil;

    private static final long EXPIRE_HOURS = 1;


    /*反正这块代码有很大问题，需要修改*/

    /**
     * 根据用户名和密码进行登陆验证，并返回登录后的User对象。如果验证成功，则返回200 OK和User对象；否则返回401 Unauthorized和错误信息。
     * @param loginBody
     * @return Result<Object>
     */
    @Override
    public Result<Object> login(LoginBody loginBody) {

        Result<Object> objectResult = captchaService.checkCaptcha(loginBody);
        if (!objectResult.getCode().equals("200")){//验证码不正确
            return new Result<>("500",objectResult.getMsg(),null);
            //先查询redis是否已存在uuid的token
        } else if (userMapper.queryByIdAndPwd(Integer.parseInt(loginBody.getId()),loginBody.getPassword())!=1){
            return new Result<>("500","用户名或密码不正确",null);
        }else{
            String token = tokenUil.generateToken(loginBody.getId(),loginBody.getUuid(),loginBody.getCode());
            return new Result<>("200","登录成功",token);
        }
    }

    @Override
    public Result<Object> creatorUser(String uname, String password, String email,
                                      int age, String gender) {
        // 校验用户名格式是否符合要求
        if (uname == null || uname.trim().isEmpty() || uname.length() > 20
                || !uname.matches("\\w+")) {
            return new Result<>("500","用户名格式不符合要求",null);
        }
        // 校验密码格式是否符合要求
        if (password == null || password.length() < 6 || password.length() > 20) {
            return new Result<>("500","密码长度应在6到20个字符之间",null);
        }
        // 校验邮箱格式是否符合要求
        if (email == null || !email.matches("\\w+@\\w+\\.\\w+")) {
            return new Result<>("500","邮箱格式不符合要求",null);
        }
        // 校验年龄格式是否符合要求
        if (age <= 0 || age > 200) {
            return new Result<>("500","年龄应该是一个正整数且不能超过200岁",null);
        }
        // 校验性别格式是否符合要求
        if (!"男".equals(gender) && !"女".equals(gender)) {
            return new Result<>("500","性别必须是男或女",null);
        }
        // 校验用户名是否已存在
        if (userMapper.queryName(uname) != null) {
            return new Result<>("500","用户名已被占用",null);
        }
        // 校验邮箱是否已存在
        else if (userMapper.queryEmail(email) != null) {
            return new Result<>("500","邮箱已被占用",null);
        }
        // 创建用户
        else {
            userMapper.creatorUser(uname, password, email, age, gender);
            User user = userMapper.queryByName(uname);
            return new Result<>("200","注册成功",user);//仅包含uid的user对象
        }
    }


}

